/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.service;

import br.com.foxline.clinicplus.entity.Category;
import br.com.foxline.clinicplus.entity.TemplateAppraisal;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
public class TemplateAppraisalService extends GenericService<TemplateAppraisal> {

    public TemplateAppraisalService() {
        super(TemplateAppraisal.class);
    }

    public List<TemplateAppraisal> find(TemplateAppraisal templateAppraisal) {
        String sql = "SELECT t FROM TemplateAppraisal t WHERE";

        if (templateAppraisal.getName() != null) {
            sql += " lower(t.name) like lower(:name) ";
        }

        Query q = getEntityManager().createQuery(sql);

        if (templateAppraisal.getName() != null) {
            q.setParameter("name", "%" + templateAppraisal.getName() + "%");

        }

        return (List<TemplateAppraisal>) q.getResultList();
    }

    public List<Category> getCategorys(TemplateAppraisal templateAppraisal) {
        String sql = "SELECT c FROM TemplateAppraisal t JOIN t.categorys c JOIN c.items i WHERE";

        if (templateAppraisal.getName() != null) {
            sql += " t.id = :id ";
        }

        sql += "group by c.id order by c.id asc ";
        
        Query q = getEntityManager().createQuery(sql);

        if (templateAppraisal.getName() != null) {
            q.setParameter("id", templateAppraisal.getId());

        }

        return (List<Category>) q.getResultList();
    }
}
